//
//  WYCQRCodeController.m
//  WYCweibo
//
//  Created by 魏云超 on 15/11/11.
//  Copyright (c) 2015年 魏云超. All rights reserved.
//

#import "WYCQRCodeController.h"
#import <AVFoundation/AVFoundation.h>
@interface WYCQRCodeController ()<AVCaptureMetadataOutputObjectsDelegate,UIAlertViewDelegate>
- (IBAction)close:(id)sender;
//冲击波顶部约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topCons;
@property (nonatomic, strong) CADisplayLink *link;
@property (nonatomic, strong) AVCaptureSession *session;

@end

@implementation WYCQRCodeController
bool isOpenQR = false;
- (void)viewDidLoad {
    [super viewDidLoad];
    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
//    [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    self.link = link;
    //1.获取输入设备
    AVCaptureDevice *divice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    //2.根据输入设备创建对象
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:divice error:NULL];
    //3.创建输出对象
    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc]init];
    //4.设置代理监听输出对象数据
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    //5.创建回话
    AVCaptureSession *session = [[AVCaptureSession alloc]init];
    //6.添加输入对象输出对象到回话中
    if ([session canAddInput:input]) {
        [session addInput:input];
    }
    if ([session canAddOutput:output]) {
        [session addOutput:output];
    }
    if (divice == nil) {
        return;
    }
    //7.告诉输出对象输出什么数据
    [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
    //7.1 创建预览图层
    AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
    previewLayer.frame = self.view.frame;
    [self.view.layer insertSublayer:previewLayer atIndex:0];
    //8.开始扫描数据
    [session startRunning];
    self.session = session;
}

//监听数据 扫描到数据会调用
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
//    DDLogInfo(@"扫描到数据啦!");
    if (metadataObjects.count > 0) {
        //扫描到数据停止扫描
        [self.session stopRunning];
        //冲击波停止
        if (isOpenQR == false) {
            [self.link removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
        }
        isOpenQR = true;
        //获取扫描结果
        AVMetadataMachineReadableCodeObject *obj = [metadataObjects lastObject];
//        obj.stringValue
        NSString *str = obj.stringValue;
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"扫描结果" message:str delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        
        [alert show];
    }
}
-(void)viewWillAppear:(BOOL)animated
{
    //开启定时器
    [self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
-(void)viewWillDisappear:(BOOL)animated{
    if (isOpenQR == false) {
        [self.link removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    }
}
-(void)update{
    self.topCons.constant -= 1;
    if (self.topCons.constant <= -170) {
        self.topCons.constant = 170;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)close:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
